package ldh.im.fxgui.demo;

import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.print.PageLayout;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SnapshotParameters;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.image.ImageView;
import javafx.scene.image.WritableImage;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.robot.Robot;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;
import ldh.im.fxbase.component.ScreenCapturePane;
import ldh.im.fxbase.service.AudioCommonService;
import ldh.im.fxbase.service.AudioService;
import ldh.im.fxbase.service.AudioStreamService;
import ldh.im.fxbase.util.StageUtil;

public class SecondStageDemo extends Application {

    private ScreenCapturePane screenCapturePane = new ScreenCapturePane();
    private ImageView fullScreenImageView = new ImageView();
    private Robot robot = new Robot();
    private Screen screen = null;

    @Override
    public void start(Stage primaryStage) throws Exception {
        StageUtil.setStage(primaryStage);
        screen = Screen.getPrimary();

        BorderPane borderPane = new BorderPane();
        VBox vBox = buildLeftPane();
        borderPane.setLeft(vBox);
        HBox top = new HBox();
        top.getChildren().addAll(new Label("demo"));
        borderPane.setTop(top);

        Scene scene = new Scene(borderPane, 800, 600);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private VBox buildLeftPane() {
        VBox vBox = new VBox(10);
        Button newStageBtn = createBtn("second stage", newStage());
        Button captruePane = createBtn("capture pane", captruePane());
        Button errorClick = createBtn("errorClick", errorClick());
        vBox.getChildren().addAll(newStageBtn, captruePane, errorClick);
        return vBox;
    }

    private Button createBtn(String text, EventHandler<ActionEvent> actionEvent) {
        Button button = new Button(text);
        button.setOnAction(actionEvent);
        return button;
    }

    private EventHandler<ActionEvent> newStage() {
        return e->{
            Stage newStage = new Stage();
            newStage.initStyle(StageStyle.UNDECORATED);
            newStage.initOwner(StageUtil.getStage());
            newStage.setMaximized(true);
            VBox anchorPane = new VBox(20);
            Label label = new Label("adsfasdfasdfasfasfasfasfasfasfas");
            Button b = new Button("capture");
            anchorPane.getChildren().addAll(fullScreenImageView, label, b);
//            anchorPane.setStyle("-fx-background-color: blue");
//            fullScreenImageView.setImage(captureScreen(label));
//            fullScreenImageView.setOpacity(0.6);
            b.setOnAction(e1->{
                fullScreenImageView.setImage(captureScreen(label));
            });
            Scene scene = new Scene(anchorPane);
            scene.setOnMouseClicked(e1-> {
                if (e1.getClickCount() == 2) newStage.setMaximized(false);
            });
            newStage.setScene(scene);
            newStage.show();
        };
    }

    private EventHandler<ActionEvent> captruePane() {
        return e->{
            screenCapturePane.show((t)->{
                System.out.println("afdasdfaf:" + t);
            });
        };
    }

    private EventHandler<ActionEvent> errorClick() {
        return e->{
            String file = "E:\\test\\audio";
            AudioService audioService = new AudioCommonService(file);
            audioService.playRecord("E:\\test\\audio\\2022102121070213.wav", (Void)->{});
        };
    }

    private WritableImage captureScreen(Node node) {
        double nw = screen.getBounds().getWidth();
        double nh = screen.getBounds().getHeight();
//        return robot.getScreenCapture(null, 0, 0, nw, nh, false);

        return node.snapshot(new SnapshotParameters(), null);
    }
}
